package com.pig4cloud.pigx.strategy.bo;

import cn.hutool.core.bean.BeanUtil;
import com.pig4cloud.pigx.common.customize.exception.MqNotAckException;
import com.pig4cloud.pigx.strategy.entity.StrategyPositionContract;
import com.pig4cloud.pigx.strategy.entity.StrategyStopOrderContract;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 策略任务-合约缓存BO
 *
 * @since 2022/11/28 20:25
 */
@Data
public class StrategyTaskContractCacheBO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    private Long id;

    /**
     * 开关id
     */
    private Long switchId;

    /**
     * 策略id
     */
    private Long strategyId;

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 首次补仓方向（0做多、1做空、2尚未补仓）
     */
    private String firstAddSide;

    /**
     * 补仓次数
     */
    private Integer addCount;

    /**
     * 下单次数
     */
    private Integer orderCount;

    /**
     * 利润（USDT）
     */
    private BigDecimal profit;

    /**
     * 预扣燃油费
     */
    private BigDecimal fuelFrozen;

    /**
     * 燃油费花费（UNT）
     */
    private BigDecimal fuelCost;

    /**
     * 委托中订单数量
     */
    private Integer pendingOrderNum;

    /**
     * 同步中止盈止损订单数量
     */
    private Integer syncStopOrderNum;

    /**
     * 正向止盈平仓数量
     */
    private Integer forwardStopCloseNum;

    /**
     * 正向止盈回滚数量
     */
    private Integer forwardStopRollbackNum;


    /**
     * 状态（0暂停、1运行中、2已完成、3用户关闭、4异常）
     */
    private String status;

    /**
     * 最高价格
     */
    private BigDecimal highestPrice = BigDecimal.ZERO;
    /**
     * 最低价格
     */
    private BigDecimal lowestPrice = BigDecimal.ZERO;
    /**
     * 正向止盈最高价格
     */
    private BigDecimal forwardStopHighestPrice = BigDecimal.ZERO;
    /**
     * 正向止盈最低价格
     */
    private BigDecimal forwardStopLowestPrice = BigDecimal.ZERO;
    /**
     * 最高价格波动
     */
    private BigDecimal highestFluctuationRatio = BigDecimal.ZERO;
    /**
     * 最高盈利
     */
    private BigDecimal highestProfit = BigDecimal.ZERO;
    /**
     * 最高盈利率
     */
    private BigDecimal highestProfitRate = BigDecimal.ZERO;
    /**
     * 上次开仓成交均价
     */
    private BigDecimal lastOpenAvg = BigDecimal.ZERO;

    /**
     * 仓位集合
     */
    List<StrategyPositionContract> positionList;

    /**
     * 止盈止损集合
     */
    List<StrategyStopOrderContractCacheBO> stopOrderList;

    /**
     * 向任务缓存中添加止盈止损
     *
     * @param stopOrder 止盈止损
     * @since 2023/2/10 10:15
     */
    public void addStopOrderCache(StrategyStopOrderContract stopOrder) {
        StrategyStopOrderContractCacheBO cache = BeanUtil.copyProperties(stopOrder, StrategyStopOrderContractCacheBO.class);
        if (Objects.isNull(stopOrderList)) {
            stopOrderList = new ArrayList<>();
        }
        stopOrderList.add(cache);
    }

    /**
     * 从任务缓存中获取止盈止损
     *
     * @param stopOrderId 止盈止损id
     * @return {@link StrategyStopOrderContract}
     * @since 2023/2/10 10:15
     */
    public StrategyStopOrderContractCacheBO getStopOrderCache(Long stopOrderId) {
        for (StrategyStopOrderContractCacheBO bo : stopOrderList) {
            if (Objects.equals(bo.getId(), stopOrderId)) {
                return bo;
            }
        }

        String errorMsg = String.format("数据异常，未能在任务缓存中找到止盈止损数据。任务id：%s，止盈止损id：%s",
                id,
                stopOrderId);
        throw new MqNotAckException(errorMsg);
    }

    /**
     * 移除任务缓存中指定的止盈止损
     *
     * @param stopOrderId 止盈止损id
     * @since 2023/2/9 19:22
     */
    public void removeStopOrderCache(Long stopOrderId) {
        stopOrderList = stopOrderList.stream()
                .filter(stopOrder -> !Objects.equals(stopOrder.getId(), stopOrderId))
                .collect(Collectors.toList());
    }
}
